package server.gui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

import server.control.ServerControl;

import entity.Constants;

public class ControlPanel extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 2271652099649974693L;
	
	private ServerControl serverCtrl;

	public ControlPanel(ServerControl ctrl) {
		
		this.setPreferredSize(new Dimension(Constants.PANEL_C_WIDTH, Constants.PANEL_C_HEIGHT));
		this.setLayout(new FlowLayout(FlowLayout.CENTER));
		this.setVisible(true);
		
		serverCtrl = ctrl;
		init();
	}
	
	private void init() {
		
		final JButton btnStart = new JButton("Thả cá");
		final JButton btnPause = new JButton("Thu cá");
		
		btnStart.setPreferredSize(new Dimension(Constants.C_BUTTON_WIDTH, Constants.C_BUTTON_HEIGHT));
		btnPause.setPreferredSize(new Dimension(Constants.C_BUTTON_WIDTH, Constants.C_BUTTON_HEIGHT));
		
		this.add(btnStart);
		this.add(btnPause);
		
		//add handle event to button
		ActionListener start = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				btnStart.setEnabled(false);
				btnPause.setEnabled(true);
				handleStartEvent();
				
			}
		};
		
		ActionListener pause = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				btnStart.setEnabled(true);
				btnPause.setEnabled(false);
				handlePauseEvent();
				
			}
		};
		
		btnStart.addActionListener(start);
		btnPause.addActionListener(pause);
	}
	
	private void handleStartEvent() {
		serverCtrl.init();
		serverCtrl.isRunning = true;
	}
	
	private void handlePauseEvent() {
		serverCtrl.fishes.removeAll(serverCtrl.fishes);
		serverCtrl.isRunning = false;
	}
}
